import {
    getToll,
    tijiao
} from "../../api/home"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        personInfo: null,
        ticketCurrent: 0,
        form: null,
        // 票档列表
        ticketData: [{
                id: 1,
                name: '单人成人票100元'
            },
            {
                id: 2,
                name: '学生票/老年票/儿童票50元'
            }
        ],
        sj: [],
        touristCurrent: 0,
        // 游客列表
        touristData: [],
        // 门票数量
        ticketCount: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            touristData: wx.getStorageSync('personArray')
        })
        if (options.id) {
            // 请求提交订单页面信息
            this.getPageInfo(options.id)
        }
    },
     
    // 请求提交订单页面信息
    async getPageInfo(id) {
        const res = await getToll({
            id: id
        })
        console.log(res);
        this.setData({
            sj: res.data,
            ticketData:res.data.ticketTypeList
        })
        // console.log(this.data.sj);
        // console.log(this.data.ticketData);
    },
    // 票档选中
    handleTicketActive(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            ticketCurrent: index
        })
    },
    // 游客选中
    handleTouristActive(e) {
        console.log(e);
        const index = e.currentTarget.dataset.index;
        console.log(index);
        this.setData({
            touristCurrent: index
        })
    },

    //返回按钮
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    },
    //新增乘客
    addnew() {
        wx.navigateTo({
            url: '../../pages/add_tourist/addTourist',
        })
    },
    //用户添加完游客之后
    onShow() {
        let personData = wx.getStorageSync('personArray');
        if (!personData) personData = [];
        let result = personData.some(item => {
            return item ? item.name : "" == this.data.personInfo ? this.data.personInfo.name : ""
        })
        // console.log(result) // false
        // if (result) return wx.showToast({
        //   title: '游客已添加过了',
        //   icon: "error"
        // })
        // 游客未填加 
        // console.log(this.data.personInfo)
        if (this.data.personInfo != null) {
            personData.push(this.data.personInfo)
        }
        wx.setStorageSync('personArray', personData) //这存的
        console.log(personData)
        console.log();
        this.setData({
            touristData: wx.getStorageSync("personArray") || []
        })

    },
    //提交表单
    submitorder() {
        const { cardNumber, name, phone } = this.data.touristData[this.data.touristCurrent];
        const { id, tollExhibitionId, price } = this.data.ticketData[this.data.ticketCurrent];
        console.log();
        tijiao({
            amount: 2.5,
            discountedPrice: 2.3,
            exhibitionName: this.data.sj.name,
            num: this.data.ticketCount,
            ticketTypeId: id,
            tollExhibitionId,
            travelers: [{
                cardNum: cardNumber,
                certType: "身份证",
                name,
                phone
            }],
            unitPrice: price
        }).then(res => {
            console.log(res);
            console.log(this.data.sj);
            wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.package,
                signType: res.data.signType,
                paySign: res.data.paySign,
                success (res) { },
                fail (res) { }
              })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})